<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property string $full_name
 * @property string $phone
 * @property string $mobile
 * @property integer $status
 * @property integer $level
 * @property string $email
 * @property string $address
 * @property string $province
 * @property integer $last_login
 * @property integer $join_time
 * @property integer $modified_time
 * @property integer $lock_from
 * @property integer $lock_to
 * @property string $avatar
 * @property string $birthday
 * @property integer $gender
 */
class User extends CActiveRecord
{
        const S_SUPPLIER         = 3;
        const S_TRANSPORTER      = 2;
        const S_DISTRIBUTOR      = 4;
        const S_DEFAULT_USER     = 1;
        const S_ADMINISTRATOR    = 5;
        
        const S_WAITING          = 1;
        const S_ACTIVED          = 2;
        const S_LOCKED           = 0;

        const S_IMAGES      = '/media/images/';
        const S_THUMBNAIL   = '/media/images/thumbnails/';
        const S_NOIMAGE     = 'noimage.gif';

                static function getTypeUser() {
            return array(
                self::S_DEFAULT_USER => 'Người dùng thường',
                self::S_SUPPLIER     => 'Nhà cung cấp',
                self::S_TRANSPORTER  => 'Đơn vị vận chuyển',                
            );
        }
	/**
	 * Returns the static model of the specified AR class.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, full_name, phone, mobile, level, email, address, province', 'required'),
			array('status, level, gender', 'numerical', 'integerOnly'=>true),
			array('username, phone, mobile', 'length', 'max'=>45),
			array('password', 'length','max'=>72,'min'=>6,'message'=>'Mật khẩu phải '),
			array('full_name, address, province', 'length', 'max'=>255),
			array('email', 'length', 'max'=>70),
                        array('email', 'email','message'=>'Email không đúng định dạng'),
                        array('email','exist',
                            'allowEmpty' => true,
                            'attributeName' => 'email',
                            'className' => 'User',
                            'message'=>'Currency must be already defined!'),
                        array('avatar', 'file',
                            'types'     => 'gif, jpg, png',
                            'maxSize'   =>1024 * 1024 * 2,
                            'tooLarge'  =>'The file was larger than 2MB. Please upload a smaller file.',
                            'allowEmpty'=>true,
                        ),
                        array('birthday', 'safe'),
			array('last_login, join_time, modified_time, lock_from', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username,full_name,level, email, address, province', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => 'Tên đại diện',
			'password' => 'Mật khẩu',
			'full_name' => 'Tên đầy đủ',
			'phone' => 'Phone',
			'mobile' => 'Mobile',
			'status' => 'Trạng thái',
			'level' => 'Đối tượng',
			'email' => 'Email',
			'address' => 'Địa chỉ',
			'province' => 'Tỉnh',
			'last_login' => 'Đăng nhập lần cuối',
			'join_time' => 'Thời gian tham gia',
			'modified_time' => 'Thời gian sửa',
			'lock_from' => 'Khóa từ',
			'lock_to' => 'Khóa tới',
                        'avatar' => 'Ảnh đại diện',
                        'birthday' => 'Sinh nhật',
                        'gender' => 'Giới tính',
		);
	}
       
         /**
         * Check user type
         * @param <type> $id
         * @param <type> $userLevel
         * @return <type>
         */
        protected function checkUserType($id,$userLevel) {
            $user = self::model()->findByPk((int)$id);
                if($userLevel == $user->level) {
                    return TRUE;
                }
                else
                    return FALSE;
        }
         /**
         * Check user status
         * @param <type> $id
         * @param <type> $userLevel
         * @return <type>
         */
        protected function checkUserStatus($id,$userStatus) {
            $user = self::model()->findByPk((int)$id);
                if($userLevel == $user->status) {
                    return TRUE;
                }
                else
                    return FALSE;
        }
        

        /**
         * Check use is locked
         * @param <int> $id
         * @return <Boolean> 
         */
        public function isLocked($id) {
            return $this->checkUserStatus($id, self::S_LOCKED);
        }
        /**
         * Check use is actived
         * @param <int> $id
         * @return <Boolean>
         */
        public function isActived($id) {
            return $this->checkUserStatus($id, self::S_ACTIVED);
        }
        /**
         * Check use is waiting
         * @param <int> $id
         * @return <Boolean>
         */
        public function isWaiting($id) {
            return $this->checkUserStatus($id, self::S_WAITING);
        }

        /**
         * Check is default User
         * @param <type> $id
         * @return Boolean
         */
        public function isDefaultUser($id) {
            return $this->checkUserType($id, self::S_DEFAULT_USER);
        }
        /**
         * Check is admin
         * @param <type> $id
         * @return Boolean
         */
        public function isAdmin($id) {
            return $this->checkUserType($id, self::S_ADMINISTRATOR);
        }
        /**
         * Check is supplier
         * @param <type> $id
         * @return Boolean
         */
        public function isSupplier($id) {
            return $this->checkUserType($id, self::S_SUPPLIER);
        }
        /**
         * Check is distributor
         * @param <type> $id
         * @return Boolean
         */
        public function isDistributor($id) {
            return $this->checkUserType($id, self::S_DISTRIBUTOR);
        }
        /**
         * Check is Transporter
         * @param <type> $id
         * @return Boolean
         */
        public function isTransporter($id) {
            return $this->checkUserType($id, self::S_TRANSPORTER);
        }
        /**
         * Check use exist with email
         * @param string $email
         * @return <type>
         */
        public function checkUserExist($email) {
            $user = self::model()->findByAttributes(array('email'=>$email));
            if(null==$user) {
                return FALSE;
            }
            else
                return TRUE;
        }

        /**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('full_name',$this->full_name,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('level',$this->level);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('province',$this->province,true);
		$criteria->compare('last_login',$this->last_login,true);
		$criteria->compare('join_time',$this->join_time,true);
		$criteria->compare('modified_time',$this->modified_time,true);
		$criteria->compare('lock_from',$this->lock_from,true);
		$criteria->compare('lock_to',$this->lock_to);
                $criteria->compare('avatar',$this->avatar,true);
                $criteria->compare('birthday',$this->birthday,true);
                $criteria->compare('gender',$this->gender);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
                        'pagination'=>array(
                                    'pageSize'=>'10',
                            ),
		));
	}
}